package xyz.liut.autolaunch;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import xyz.liut.logcat.L;

/**
 * 支付宝服务启动
 * <p>
 * Create by liut on 20-10-29
 */
public class AliPayLaunch {

    private static final String TAG = "AliPayLaunch";

    public static final String ALIPAY_PKG = "com.eg.android.AlipayGphone";
    public static final String ALIPAY_SERVICE = "com.alipay.android.launcher.service.LauncherService";


    /**
     * 启动支付宝服务
     */
    public static void launch(Context context) {
        Log.d(TAG, "launch() called");
        new Thread() {
            @Override
            public void run() {
                String cmd;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    cmd = "su -c am start-foreground-service " + ALIPAY_PKG + "/" + ALIPAY_SERVICE;
                } else {
                    cmd = "su -c am startservice " + ALIPAY_PKG + "/" + ALIPAY_SERVICE;
                }
                ProcessUtil.Result result = ProcessUtil.exec(cmd);
                if (result != null) {
                    result.println();
                    L.d("支付宝服务启动: " + (result.exitValue == 0));
                    new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, result.isText, Toast.LENGTH_SHORT).show());
                } else {
                    L.e("支付宝服务启动失败");
                }
            }
        }.start();
    }

}
